﻿$(function () {

    $("#userpanel-menulist > li:eq(1)").addClass("sel");

    $("#addrpostform").ajaxForm({
        dataType: "json",
        success: saveCallBack
    });

    function reloadtb() {
        layui.use("table", function () {
            var table = layui.table;
            table.init('dtb-addr', {
                page: false
            });
        });
    }
    function del(id,obj) {
        var id = parseInt(tr.attr("data-id"));
        $.post({
            url: "/SpAddr/Remove",
            data: { id: id },
            dataType: "json",
            success: function (res) {
                if (res.Isucc) {
                    layer.msg("删除成功!", {
                        time: 1500,
                        end: function () {
                            location.reload();
                        }
                    });
                }
                else {
                    layer.msg("删除失败\n 错误信息:" + res.Errors[0], {
                        icon: 3,
                    });
                }
            }
        })
    }


    function setdef(id,obj) {
        $.post({
            url: "/SpAddr/SetDefault",
            data: { id: id },
            dataType: "json",
            success: function (res) {
                if (res.Isucc) {
                    location.reload();
                }
            }
        })
    }
    layui.use("table", function () {
        var table = layui.table;
        table.init('dtb-addr', {
            page: false
        });
        table.on("tool(dtb-addr)", function (obj) {
            var rowdata = obj.data;
            var layEvent = obj.event;
            var id = rowdata.id;
            if (layEvent == "del") {
                del(id,obj);
            }
            if (layEvent == "setdef") {
                setdef(id,obj);
            }
        });
    });

    function saveCallBack(data) {
        if (data.Isucc) {
            layer.msg('添加成功!', {
                icon: 1,
                area: "200px",
                time: 2000,
                end: function () {
                    location.reload();
                }
            })
        }
        else {
            layer.msg("添加失败!" + data.ErrorMsg, {
                icon: 2,
                time: 2000,
                end: function () {
                    $("#addrpostform")[0].reset();
                    layui.form.render();
                }
            })
        }
    }
});